class EmailTemplate < ActiveRecord::Base
  set_table_name :eshop_email_templates

  liquid_methods :current_user

  paginates_per 15
  
  validates :name, :presentation, :length => {:maximum => 50}, :presence => true
  validates :name, :uniqueness => true, :format => {:with => Format::STRING_REGEX}
  validates :subject, :length => {:maximum => 150}, :presence => true
  validates :from, :bcc, :allow_blank => true, :format => {:with => Format::EMAIL_REGEX}, :length => {:maximum => 50}
  validates :template, :presence => true
  validates_with LiquidTemplateFormat

  has_paper_trail
  diff :exclude => [:id, :created_at, :updated_at]

  def current_user
    current_user
  end

  #def body=(template)
    #self.body = template

    #begin
      #@template = Liquid::Template.parse self.body
      #self.template = Marshal.dump(@template)
    #rescue Liquid::SyntaxError => error
      #@syntax_error = error.message
    #end
  #end

#  private
#  def template
#    return @template unless @template.nil?
#
#    if self.template.blank?
#      @template = Liquid::Template.parse self.body
#      self.template = Marshal.dump @template
#      save
#    else
#      @template = Marshal.load self.template
#    end
#
#    @template
#  end
end
